
#include "mainwindow.h"

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QListWidget>
#include <QScrollArea>
#include <QCheckBox>
#include <QTextEdit>

#include <NetworkRequest>
#include <QtPrivateDeclar>

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
	QHBoxLayout *layout = new QHBoxLayout(this);
	
    QVBoxLayout *Leftlayout = new QVBoxLayout();
// left

// 1.1
    QGroupBox *leftBox = new QGroupBox("文件搜索");
    QVBoxLayout *leftBoxLayout = new QVBoxLayout(leftBox);
    Leftlayout->addWidget(leftBox);

    QLabel *searchKey = new QLabel("文件:");
    QLineEdit *searchEdit = new QLineEdit;
    QLabel *searchUniqueLabel = new QLabel("选项:");
    QCheckBox *unique_package = new QCheckBox("unique packages?");
    QPushButton *searchButton = new QPushButton("Search");
    QListWidget *resultView = new QListWidget;
    unique_package->setCheckState(Qt::CheckState::Checked);
    
    QGridLayout *searchBoxLayout = new QGridLayout;
    searchBoxLayout->addWidget(searchKey, 0, 0);
    searchBoxLayout->addWidget(searchEdit, 0, 1);
    searchBoxLayout->addWidget(searchButton, 0, 2);

    searchBoxLayout->addWidget(searchUniqueLabel, 1, 0);
    searchBoxLayout->addWidget(unique_package, 1, 1);
    
    // 左面布局
    leftBoxLayout->addLayout(searchBoxLayout);
    leftBoxLayout->addWidget(resultView);

// ----------------------------------------------------------------    

    QVBoxLayout *Rightlayout = new QVBoxLayout();
// right

// 2.1
    QGroupBox *rightBox = new QGroupBox("信息展示");
    QVBoxLayout *rightBoxLayout = new QVBoxLayout(rightBox);
    Rightlayout->addWidget(rightBox);

    QLabel *categoryLabel = new QLabel("Category:");
    QLineEdit *categoryEdit = new QLineEdit;
    QLabel *packageLabel = new QLabel("Package:");
    QLineEdit *packageEdit = new QLineEdit;
    QLabel *pathLabel = new QLabel("Path:");
    QLineEdit *pathEdit = new QLineEdit;
    QLabel *fileLabel = new QLabel("File:");
    QLineEdit *fileEdit = new QLineEdit;
    QLabel *versionLabel = new QLabel("Version:");
    QLineEdit *versionEdit = new QLineEdit;


    QLabel *archsLabel = new QLabel("Archs:");
    QTextEdit *archsEdit = new QTextEdit;
    QLabel *usesLabel = new QLabel("USE flags:");
    QTextEdit *usesEdit = new QTextEdit;

    archsEdit->setBackgroundRole(QPalette::ColorRole::Window);
    usesEdit->setBackgroundRole(QPalette::ColorRole::NoRole);


    QGridLayout *gridLayout = new QGridLayout;
    gridLayout->addWidget(categoryLabel,0,0);
    gridLayout->addWidget(categoryEdit,0,1);

    gridLayout->addWidget(packageLabel,1,0);
    gridLayout->addWidget(packageEdit,1,1);

    gridLayout->addWidget(pathLabel,2,0);
    gridLayout->addWidget(pathEdit,2,1);

    gridLayout->addWidget(fileLabel,3,0);
    gridLayout->addWidget(fileEdit,3,1);

    gridLayout->addWidget(versionLabel,4,0);
    gridLayout->addWidget(versionEdit,4,1);

    gridLayout->addWidget(archsLabel,5,0, Qt::AlignTop);
    gridLayout->addWidget(archsEdit,5,1);
    
    gridLayout->addWidget(usesLabel,6,0,Qt::AlignTop);
    gridLayout->addWidget(usesEdit,6,1);


    rightBoxLayout->addLayout(gridLayout);
    rightBoxLayout->addStretch();

    layout->addLayout(Leftlayout,1);
    layout->addLayout(Rightlayout,1);
    layout->setStretch(0, 4);
    layout->setStretch(1, 6);

    resize(900, 600);



// ----------------------------------------------------------------
    NetworkRequest *request = new NetworkRequest();
    auto findRequest = [=](QString path){
        QString url = QString("https://portagefilelist.de/site/query/robotFile?file=%1%2").arg(path).arg(unique_package->isChecked()?"&unique_packages":"");
        request->requestString(url);
    };
    
    // searchEdit
    // connect(searchEdit, &QLineEdit::textChanged, [=](const QString &text){
    //     findRequest(text);
    // });
    // searchButton
    connect(searchButton, &QPushButton::clicked, [=](){
        findRequest(searchEdit->text());
    });

    connect(request, &NetworkRequest::replyRequest, [=](const QString &content){
        resultView->clear();
        QJsonDocument documment = QJsonDocument::fromJson(content.toLocal8Bit());
        if (documment.isObject()) {
            QJsonObject obj = documment.object();
            if (obj.contains("result")) {
                QJsonArray array = obj.value("result").toArray();
                for (int i = 0; i < array.count(); i++)
                {
                    QtPrivateDeclar package = QtPrivateDeclar(array[i].toObject());
                    QListWidgetItem *item = new QListWidgetItem();
                    item->setData(1, package.getCategory());
                    item->setData(2, package.getPackage());
                    item->setData(3, package.getPath());
                    item->setData(4, package.getFile());
                    item->setData(5, package.getVersion());
                    item->setData(6, package.getArchs());
                    item->setData(7, package.getUseflags());
                    const QString atext = QString("%1/%2")
                                            .arg(package.getCategory())
                                            .arg(package.getPackage());

                    item->setText(atext);
                    resultView->addItem(item);
                }
            }
        }
    });
    connect(resultView, &QListWidget::currentItemChanged, [=](QListWidgetItem *current) {
        if (current == nullptr) return;
        categoryEdit->setText(current->data(1).toString());
        packageEdit->setText(current->data(2).toString());
        pathEdit->setText(current->data(3).toString());
        fileEdit->setText(current->data(4).toString());
        versionEdit->setText(current->data(5).toString());
        archsEdit->setText(current->data(6).toStringList().join(","));
        usesEdit->setText(current->data(7).toStringList().join(","));
    });
    
}
MainWindow::~MainWindow()
{
	
}
